package org.dromara.legal.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.legal.domain.vo.RevertVo;
import org.dromara.legal.domain.bo.RevertBo;
import org.dromara.legal.service.IRevertService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 回复
 *
 * @author zhuqingrun
 * @date 2024-10-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/legal/revert")
public class RevertController extends BaseController {

    private final IRevertService revertService;

    /**
     * 查询回复列表
     */
    @SaCheckPermission("legal:revert:list")
    @GetMapping("/list")
    public TableDataInfo<RevertVo> list(RevertBo bo, PageQuery pageQuery) {
        return revertService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出回复列表
     */
    @SaCheckPermission("legal:revert:export")
    @Log(title = "回复", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RevertBo bo, HttpServletResponse response) {
        List<RevertVo> list = revertService.queryList(bo);
        ExcelUtil.exportExcel(list, "回复", RevertVo.class, response);
    }

    /**
     * 获取回复详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("legal:revert:query")
    @GetMapping("/{id}")
    public R<RevertVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(revertService.queryById(id));
    }

    /**
     * 新增回复
     */
    @SaCheckPermission("legal:revert:add")
    @Log(title = "回复", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RevertBo bo) {
        return toAjax(revertService.insertByBo(bo));
    }

    /**
     * 修改回复
     */
    @SaCheckPermission("legal:revert:edit")
    @Log(title = "回复", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RevertBo bo) {
        return toAjax(revertService.updateByBo(bo));
    }

    /**
     * 删除回复
     *
     * @param ids 主键串
     */
    @SaCheckPermission("legal:revert:remove")
    @Log(title = "回复", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(revertService.deleteWithValidByIds(List.of(ids), true));
    }
}
